package com.byb.server.repository;

import com.byb.server.domain.WcProduct;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import org.springframework.data.jpa.repository.*;

import java.util.List;


/**
 * Spring Data JPA repository for the WcProduct entity.
 */
@SuppressWarnings("unused")
@Repository
public interface WcProductRepository extends JpaRepository<WcProduct, Long> {

    @Query("select p from WcProduct p where p.wcIsurer.id=:insurerId and p.wcInsuranceCategory.id=:categoryId order by p.ranking desc ")
    Page<WcProduct> findAllByWcIsurerAndWcInsuranceCategory(@Param("insurerId") Long insurerId, @Param("categoryId") Long categoryId, Pageable pageable);

    @Query("select p from WcProduct p where p.wcIsurer.id=:insurerId")
    Page<WcProduct> findAllByWcIsurer(@Param("insurerId") Long insurerId,Pageable pageable);

    @Query("select p from WcProduct p where p.wcInsuranceCategory.id=:categoryId order by p.ranking desc ")
    Page<WcProduct> findAllByWcInsuranceCategory(@Param("categoryId") Long categoryId,Pageable pageable);

    @Query("select p.wcIsurer.id from WcProduct p group by p.wcIsurer")
    List<Long> findGroupByWcInsurer(Pageable pageable);

    @Query(value = "select p.* from wc_product p where p.wc_insurance_category_id=?1 order by LENGTH(p.ranking), p.ranking ASC  limit 0,10",nativeQuery = true)
    List<WcProduct> findAllLime20( Long categoryId);

    WcProduct findById(Long id);
}
